/*
* 作业许可列表
* */

import 'dart:convert';

import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/config/routeConfig.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class WorkPermitList extends StatefulWidget {
  const WorkPermitList();

  @override
  _WorkPermitListState createState() => _WorkPermitListState();
}

class _WorkPermitListState extends State<WorkPermitList> {
  EasyRefreshController _controller = EasyRefreshController();
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  List dataSource = [];
  int page = 1;
  int pageSize = 15;
  bool firstLoad = true;

  @override
  void initState() {
    super.initState();
    this.getAllReceived();
  }

  @override
  void dispose() {
    _controller.dispose();
    loading.dismiss();
    super.dispose();
  }

  /*
  * 获取受限空间作业列表
  * */
  getAllReceived() async {
    var pass = {
      'queryFields': json.encode([
        {
          "columnName": 'projectSectionId',
          "dataType": "string",
          "operator": "=",
          "queryValue": GlobalConfig.selectFactory.tag,
        }
      ]),
      'page': page,
      'limit': pageSize,
    };
    var result = await getConsWorkLicenseExList(pass);
    if (this.page == 1) {
      dataSource.clear();
    }
    firstLoad = false;
    if (result != null && result['hasError'] == null) {
      this.setState(() {
        if (result['results'].length == 0) {
          _controller.finishLoad(success: true, noMore: true);
        } else {
          dataSource.addAll(result['results']);
          if (dataSource.length == result['totalRecordNum']) {
            _controller.finishLoad(success: true, noMore: true);
          }
        }
      });
    } else {
      _controller.finishLoad(success: true);
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('施工作业'),
      ),
      body: Container(
        color: Styles.MainBackgroundColor,
        padding: EdgeInsets.fromLTRB(
            0, 0, 0, MediaQuery.of(context).padding.bottom),
        child: NotificationListener(
          onNotification: (_) {
            FocusScope.of(context).requestFocus(new FocusNode());
            return false;
          },
          child: EasyRefresh.custom(
              shrinkWrap: true,
              firstRefresh: true,
              controller: _controller,
              header: MaterialHeader(),
              footer: ClassicalFooter(
                  loadingText: '加载中',
                  loadText: '上拉加载',
                  noMoreText: '没有更多数据',
                  loadedText: '加载完成',
                  infoText: '更新于 %T',
                  textColor: Styles.NoMoreColor,
                  showInfo: false),
              emptyWidget: (dataSource.length == 0 && !firstLoad)
                  ? Center(
                child: Image(
                  image: AssetImage('assets/images/no_data.png'),
                  width: 128,
                  height: 150,
                ),
              )
                  : null,
              onRefresh: () async {
                _controller.finishLoad(success: true, noMore: false);
                if (mounted) {
                  page = 1;
                  this.getAllReceived();
                }
              },
              onLoad: () async {
                await Future.delayed(Duration(seconds: 2), () {
                  if (mounted) {
                    page++;
                    this.getAllReceived();
                  }
                });
              },
              slivers: <Widget>[
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                        (context, index) {
                      return GestureDetector(
                        onTap: () {
                          // Get.toNamed(RouteConfig.confinedSpaceDetail2,arguments: {
                          //   'businessId':this.dataSource[index]['id'],
                          // });
                        },
                        child: Container(
                          color: Colors.white,
                          margin: EdgeInsets.fromLTRB(
                              0, Styles.spacing8, 0, 0),
                          padding: EdgeInsets.fromLTRB(
                              Styles.spacing12,
                              Styles.spacing8,
                              Styles.spacing12,
                              Styles.spacing8),
                          child: Column(
                            crossAxisAlignment:
                            CrossAxisAlignment.start,
                            children: [
                              Container(
                                padding: EdgeInsets.fromLTRB(
                                    0, Styles.spacing4, 0, 0),
                                child: Text(
                                  '${this.dataSource[index]['projectName']??'--'}',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize:
                                      Styles.MiddleFont17),
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.fromLTRB(
                                    0, Styles.spacing4, 0, 0),
                                child: Text(
                                  '分部分项工程名称：${this.dataSource[index]['partProjectName']??'--'}',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize:
                                      Styles.NormalFont),
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.fromLTRB(
                                    0, Styles.spacing4, 0, 0),
                                child: Text(
                                  '申请时间：${this.dataSource[index]['applyTimeStr']??'--'}',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize:
                                      Styles.NormalFont),
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.fromLTRB(
                                    0, Styles.spacing4, 0, 0),
                                child: Text(
                                  '计划施工时间：${this.dataSource[index]['planWorkTimeStr']??'--'}',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize:
                                      Styles.NormalFont),
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                    childCount: dataSource.length,
                  ),
                ),
              ]),
        ),
      ),);
  }
}
